<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Returns the PHP representation of a BSON value</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mongodb.bson-tojson.html">« MongoDB\BSON\toJSON</a></li>
      <li style="float: right;"><a href="function.mongodb.bson-torelaxedextendedjson.html">MongoDB\BSON\toRelaxedExtendedJSON »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.bson.functions.html">函数</a></li>
    <li>Returns the PHP representation of a BSON value</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mongodb.bson-tophp" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB\BSON\toPHP</h1>
  <p class="verinfo">(mongodb &gt;=1.0.0)</p><p class="refpurpose"><span class="refname">MongoDB\BSON\toPHP</span> &mdash; <span class="dc-title">Returns the PHP representation of a BSON value</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.mongodb.bson-tophp-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>MongoDB\BSON\toPHP</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$bson</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$typeMap</code><span class="initializer"> = array()</span></span>): <span class="type"><span class="type">array</span>|<span class="type">object</span></span></div>

  <p class="para rdfs-comment">
   Unserializes a BSON document (i.e. binary string) to its PHP representation.
   The <code class="parameter">typeMap</code> paramater may be used to control the PHP
   types used for converting BSON arrays and documents (both root and embedded).
  </p>
  
   <div class="warning"><strong class="warning">警告</strong>
    <p class="simpara">
     BSON documents can technically contain duplicate keys because documents are
     stored as a list of key-value pairs; however, applications should refrain
     from generating documents with duplicate keys as server and driver behavior
     may be undefined. Since PHP objects and arrays cannot have duplicate keys,
     data could also be lost when decoding a BSON document with duplicate keys.
    </p>
   </div>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.mongodb.bson-tophp-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">bson</code> (<span class="type">string</span>)</dt>

    <dd>

     <p class="para">
      BSON value to be unserialized.
     </p>
    </dd>

   
   
   
    <dt>
<code class="parameter">typeMap</code> (<span class="type">array</span>)</dt>

    <dd>

     <p class="para">
      <a href="mongodb.persistence.deserialization.html#mongodb.persistence.typemaps" class="link">Type map configuration</a>.
     </p>
    </dd>

   

  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mongodb.bson-tophp-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The unserialized PHP value.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.mongodb.bson-tophp-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="simplelist">
   <li class="member">
    Throws
    <span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.html" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span> if
    a class in the type map cannot be instantiated or does not implement
    <span class="interfacename"><a href="class.mongodb-bson-unserializable.html" class="interfacename">MongoDB\BSON\Unserializable</a></span>.
   </li>
   <li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-unexpectedvalueexception.html" class="classname">MongoDB\Driver\Exception\UnexpectedValueException</a></span> if the input did not contain exactly one BSON document. Possible reasons include, but are not limited to, invalid BSON, extra data (after reading one BSON document), or an unexpected <a href="https://github.com/mongodb/mongo-c-driver/tree/master/src/libbson" class="link external">&raquo;&nbsp;libbson</a> error.</li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.mongodb.bson-tophp-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>PECL mongodb 1.4.0</td>
       <td>
        <p class="para">
         If the input contains an unsupported, deprecated BSON type, the
         driver will now no longer log a warning to the debug log, but instead
         will create an object representing this type.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.3.2</td>
       <td>
        <p class="para">
         <span class="classname"><a href="class.mongodb-driver-exception-unexpectedvalueexception.html" class="classname">MongoDB\Driver\Exception\UnexpectedValueException</a></span>
         is no longer thrown if the input contains an unsupported, deprecated
         BSON type. Such types will be ignored (as they were in versions before
         1.3.0), although the driver will now log a warning to the debug log
         (see: <a href="mongodb.configuration.html#ini.mongodb.debug" class="link">mongodb.debug</a>).
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.3.0</td>
       <td>
        <p class="para">
         <span class="classname"><a href="class.mongodb-driver-exception-unexpectedvalueexception.html" class="classname">MongoDB\Driver\Exception\UnexpectedValueException</a></span>
         is thrown if the input contains an unsupported, deprecated BSON type.
         Previously, such types were ignored.
        </p>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mongodb.bson-tophp-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1371">
   <p><strong>示例 #1 <span class="function"><strong>MongoDB\BSON\toPHP()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$bson&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">hex2bin</span><span style="color: #007700">(</span><span style="color: #DD0000">'0e00000010666f6f000100000000'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">BSON</span><span style="color: #007700">\</span><span style="color: #0000BB">toPHP</span><span style="color: #007700">(</span><span style="color: #0000BB">$bson</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (1) {
  [&quot;foo&quot;]=&gt;
  int(1)
}
</pre></div>
   </div>
  </div>
 </div>



 <div class="refsect1 seealso" id="refsect1-function.mongodb.bson-tophp-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="function.mongodb.bson-fromphp.html" class="function" rel="rdfs-seeAlso">MongoDB\BSON\fromPHP()</a> - Returns the BSON representation of a PHP value</span></li>
   <li class="member"><a href="https://www.mongodb.com/docs/manual/reference/bson-types/" class="link external">&raquo;&nbsp;MongoDB BSON</a></li>
   <li class="member"><a href="mongodb.persistence.html" class="xref">Persisting Data</a></li>
  </ul>
 </div>


</div></div></div></body></html>